package edu.ccut.saturn.component.impl;

import edu.ccut.saturn.component.IParameterInfo;
import edu.ccut.saturn.component.IValidateUseInfo;
import edu.ccut.saturn.component.loader.node.NodeInfoLoader;
import edu.ccut.saturn.component.xml.ISaturnElement;
import edu.ccut.saturn.component.xml.SaturnElement;

/**
 * {@link IParameterInfo} implementation
 * 
 * @see IParameterInfo
 * @author LeslieGu
 */
public class SaturnParameterInfo implements IParameterInfo {

	private String id = "-1";

	private String name;

	private Class<?> classType;

	private String passingType;

	private String valueKey;

	private String description;

	private int scope;

	private IValidateUseInfo validateUses;

	public SaturnParameterInfo() {
	}

	public SaturnParameterInfo(String id, String name, Class<?> classType,
			String passingType, String valueKey, String description, int scope,
			IValidateUseInfo validateUses) {

		this.id = id;
		this.name = name;
		this.classType = classType;
		this.passingType = passingType;
		this.valueKey = valueKey;
		this.description = description;
		this.scope = scope;
		this.validateUses = validateUses;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public Class<?> getClassType() {
		return classType;
	}

	public String getPassingType() {
		return passingType;
	}

	public String getDescription() {
		return description;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setClassType(Class<?> classType) {
		this.classType = classType;
	}

	public void setPassingType(String passingType) {
		this.passingType = passingType;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getValueKey() {
		return valueKey;
	}

	public void setValueKey(String valueKey) {
		this.valueKey = valueKey;
	}

	public int getScope() {
		return scope;
	}

	public void setScope(int scope) {
		this.scope = scope;
	}

	public int compareTo(IParameterInfo parameterInfo) {

		if (this.id != null) {
			int orgId = Integer.parseInt(this.id.trim());
			int nextId = 0;

			try {
				nextId = Integer.parseInt(parameterInfo.getId().trim());

			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			return orgId > nextId ? 1 : -1;
		}
		return -1;
	}

	public String toString() {
		return this.classType + "#" + this.name;
	}

	public IValidateUseInfo getValidateUses() {
		return validateUses;
	}

	public void setValidateUses(IValidateUseInfo validateUses) {
		this.validateUses = validateUses;
	}

	public ISaturnElement toSaturnElement() {
		ISaturnElement parameterElement = new SaturnElement(
				NodeInfoLoader.KEY_PARAMETER);

		parameterElement.addAttribute("id", this.id);
		parameterElement.addAttribute("name", this.name);
		parameterElement.addAttribute("classType", this.classType.getName());
		parameterElement.addAttribute(
				NodeInfoLoader.KEY_PARAMETER_PASSING_TYPE, this.passingType);

		ISaturnElement valueKeyElement = new SaturnElement(
				NodeInfoLoader.KEY_PARAMETER_VALUE_KEY, this.valueKey);

		ISaturnElement scopeElement = new SaturnElement(
				NodeInfoLoader.KEY_PARAMETER_SCOPE, String.valueOf(this.scope));

		ISaturnElement descriptionElement = new SaturnElement(
				NodeInfoLoader.KEY_PARAMETER_DECRRIPTION, this.description);

		parameterElement.addSubElement(valueKeyElement);
		parameterElement.addSubElement(scopeElement);
		parameterElement.addSubElement(descriptionElement);

		if (this.validateUses != null) {
			parameterElement.addSubElement(this.validateUses.toSaturnElement());
		}
		return parameterElement;
	}

	public String toXmlStr() {
		ISaturnElement saturnElement = toSaturnElement();
		return saturnElement.getXMLStr();
	}
}
